package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.data.ResultEnum;
import com.dongdongshop.pojo.seller.TbBrand;
import com.dongdongshop.pojo.seller.TbSpecification;
import com.dongdongshop.pojo.seller.TbTypeTemplate;
import com.dongdongshop.service.BrandService;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.service.TemplateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("template")
public class TemplateController {
    @Reference
    private TemplateService templateService;
    @Reference
    private BrandService brandService;
    @Reference
    private SpecificationService specificationService;

    private final Logger logger= LoggerFactory.getLogger(TemplateController.class);

    @RequestMapping("toList")
    public String toList(Model model){
        List<TbBrand> brandAll=brandService.getInfo();
        List<TbSpecification> specAll=specificationService.getSpecAll();

        model.addAttribute("brandAll",brandAll);
        model.addAttribute("specAll",specAll);
        return "admin/type_template";
    }

    //查询
    @RequestMapping("listTemplate")
    @ResponseBody
    public Result listTemplate(String name){
        try {
            List<TbTypeTemplate> list=templateService.listTemplate(name);
            return Result.response(ResultEnum.OK).setData(list);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    //删除
    @RequestMapping("deleteIds")
    @ResponseBody
    public Result deleteIds(@RequestParam("ids[]")Long[] ids){
        try {
            int i=templateService.deleteIds(ids);
            return Result.response(ResultEnum.OK).setData(i);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    //增加
    @RequestMapping("addTemplate")
    @ResponseBody
    public Result addTemplate(TbTypeTemplate tbTypeTemplate){
        try {
            int i=templateService.addTemplate(tbTypeTemplate);
            if (i>0){
                return Result.response(ResultEnum.OK);
            }else {
                return Result.response(ResultEnum.FAIL);
            }
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }
    //回显
    @RequestMapping("updateById/{id}")
    @ResponseBody
    public Result updateById(@PathVariable("id")Long id){
        try {
            TbTypeTemplate tbTypeTemplate=templateService.updateById(id);
            return Result.response(ResultEnum.OK).setData(tbTypeTemplate);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }
    //修改
    @RequestMapping("updateTemplate")
    @ResponseBody
    public Result updateTemplate(TbTypeTemplate tbTypeTemplate){
        System.out.println("tem"+tbTypeTemplate);
        try {
            int i=templateService.updateTemplate(tbTypeTemplate);
            if (i>0){
                return Result.response(ResultEnum.OK);
            }else {
                return Result.response(ResultEnum.FAIL);
            }
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }
}
